package com.sx.micowell.framework.serialPort

import android.serialport.SerialPort
import com.sx.micowell.framework.arouth.RouteManager
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.security.InvalidParameterException

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/9/7 10:44
 */
class SerialPortDevices {

    companion object {
        private var instance: SerialPortDevices? = null

        @Synchronized
        fun getInstance(): SerialPortDevices {
            if (instance == null) {
                instance = SerialPortDevices()
            }
            return instance!!
        }
    }
    private var mSerialPort: SerialPort? = null

    /**获取端口*/
    fun getSerialPort(): SerialPort? {
        if (mSerialPort == null) {
            val serialPort = SerialPort //
                .newBuilder(SerialPortConfig.SP_PATH, SerialPortConfig.SP_BAUD_RATE) // 串口地址地址，波特率
                .parity(SerialPortConfig.SP_PARITY) // 校验位；0:无校验位(NONE，默认)；1:奇校验位(ODD);2:偶校验位(EVEN)
                .dataBits(SerialPortConfig.SP_DATA_BITS) // 数据位,默认8；可选值为5~8
                .stopBits(SerialPortConfig.SP_STOP_BITS) // 停止位，默认1；1:1位停止位；2:2位停止位
                .build()
            mSerialPort = serialPort
        }
        return mSerialPort
    }

    /**关闭端口*/
    fun closeSerialPort() {
        if (mSerialPort != null) {
            mSerialPort!!.close()
            mSerialPort = null
        }
    }


}